package com.clss.emergencycall.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.clss.emergencycall.R;
import com.clss.emergencycall.popAndDialog.LoadingDialog;
import com.clss.emergencycall.tools.eventbus.EventBusMessage;
import com.clss.emergencycall.utils.ACache;
import com.clss.emergencycall.utils.AppUtils;
import com.clss.emergencycall.utils.Lg;
import com.clss.emergencycall.utils.VersionUpdataHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by Liuv on 2017/4/12.
 */

public abstract class BaseFragment extends Fragment implements BaseView {
    private static final String TAG = "BaseFragment";

    private Toast ShortToast;
    protected ProgressDialog mProgressDialog;
    protected View rootView;
    public Animation fade_in;
    public Animation fade_out;
    public ACache mACache = ACache.get();
    public Context mContext;
    public LoadingDialog loadDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(setViewId(), container, false);
            EventBus.getDefault().register(this);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        mContext = getActivity();

        return rootView;
    }

    public void showLoadDialog(){
        if (loadDialog == null){
            loadDialog = AppUtils.getCricleDialog(mContext);
        }
        if (!loadDialog.isShowing())
            loadDialog.show();
    }

    public void dismissLoaddialog(){
        if (loadDialog!= null && loadDialog.isShowing())
            loadDialog.dismiss();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(rootView);
        initData();
    }

    @Override
    public void onPause() {
        super.onPause();
        doOnPause();
        getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        Lg.i("BaseFragment", "onPause");
    }

    protected abstract int setViewId();

    protected abstract void initData();

    protected abstract void initView(View v);

    protected abstract void doOnPause();

    public void logi(String s){
        Lg.i(getTag(),s);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(EventBusMessage eventBusMessage) {
        Lg.i(TAG, "---onEventMainThread===" + eventBusMessage.getCode());
    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog(null, null);
    }

    @Override
    public void showLoadingDialog(String msg) {
        showLoadingDialog(null, msg);
    }

    public void showLoadingDialog(String title, String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }

        if (title == null) {
            title = "";
        }
        if (message == null) {
            message = "";
        }

        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }


    @Override
    public void dismissLoadingDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public Context getActContext() {
        return mContext;
    }

    protected void showLongToast(int messageId) {
        showLongToast(getString(messageId));
    }

    protected void showLongToast(String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
    }

    protected void showShortToast(int messageId) {
        showShortToast(getString(messageId));
    }

    protected void showShortToast(final String message) {
        if (message.equals("`")) {
            return;
        }
        if (ShortToast == null) {
            ShortToast = Toast.makeText(getActContext(), message, Toast.LENGTH_SHORT);
        } else {
            ShortToast.setText(message);
        }
        ShortToast.show();
    }


    public VersionUpdataHelper.CustomDialog showInteractionDialog(String msg, DialogInterface.OnClickListener okListener) {
        return showInteractionDialog(msg, true, "确认", "取消", okListener, (dialog, which) -> dialog.dismiss());
    }

    public VersionUpdataHelper.CustomDialog showInteractionDialog(String msg, boolean cancelable, String ok, String no,
                                                                  DialogInterface.OnClickListener okListener,
                                                                  DialogInterface.OnClickListener noListener) {
        VersionUpdataHelper.CustomDialog.Builder builder = new VersionUpdataHelper.CustomDialog.Builder(mContext);
        builder.setMessage(msg);
        builder.setPositiveButton(ok, okListener);
        builder.setNegativeButton(no, noListener);
        VersionUpdataHelper.CustomDialog dialog = builder.create();
        dialog.setCancelable(cancelable);
        dialog.show();
        return dialog;
    }

    protected boolean checkPermission(String... permissions) {
        Lg.i(TAG, "---checkPermission===");
        if (permissions.length == 0) {
            return EasyPermissions.hasPermissions(mContext, Constant.sPermissionsArray);
        } else {
            return EasyPermissions.hasPermissions(mContext, permissions);
        }
    }

}
